﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

using YouHP.Utility;
using YouHP.Model;
using YouHP.BLL;

namespace YouHP.Pages.TreatmentHistory
{
    public class PrescriptionInputHint : YouHP.Pages.BasePage
    {
        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0)
                return;

            string hospitalKey = RequestTools.GetString("hospitalKey");
            string doctorKey = RequestTools.GetString("doctorKey");

            if (hospitalKey != "" && hospitalKey.Trim().Length > 0)
            {
                ReturnHospitalHints(hospitalKey.Trim());
            }
            else if (doctorKey != "" && doctorKey.Trim().Length > 0)
            {
                ReturnDoctorHints(doctorKey.Trim());
            }

        }   // ShowPage

        private void ReturnHospitalHints(string hospitalKey)
        {
            // Get all hospitals info.
            string condStr = string.Format(@"[Doctor].[Hospital] like '%{0}%'", hospitalKey);

            List<DoctorInfo> doctorList = DoctorBLL.Instance.GetDoctorList(condStr);

            StringBuilder sb = new StringBuilder();

            foreach (DoctorInfo info in doctorList)
            {
                sb.Append("<div onmouseover=\"onmouseOver(this);\" onmouseout=\"onmousetOut(this);\" onclick=\"setHintValueForHospital(this.innerHTML)\" class=\"onmouse_out\">");
                sb.Append(info.Hospital);
                sb.Append("</div>");
            }


            ResponseXML(sb);
        }

        private void ReturnDoctorHints(string doctorKey)
        {
            // Get all doctors info.
            string condStr = string.Format(@"[Doctor].[Name] like '%{0}%'", doctorKey);

            List<DoctorInfo> doctorList = DoctorBLL.Instance.GetDoctorList(condStr);

            StringBuilder sb = new StringBuilder();

            foreach (DoctorInfo info in doctorList)
            {
                sb.Append("<div onmouseover=\"onmouseOver(this);\" onmouseout=\"onmousetOut(this);\" onclick=\"setHintValueForDoctor(this.innerHTML)\" class=\"onmouse_out\">");
                sb.Append(info.Name);
                sb.Append("</div>");
            }


            ResponseXML(sb);
        }

        private void ResponseXML(StringBuilder sb)
        {
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.ContentType = "Text/html";
            System.Web.HttpContext.Current.Response.Expires = 0;

            System.Web.HttpContext.Current.Response.Write(sb.ToString());
            System.Web.HttpContext.Current.Response.End();
        }
    }
}
